
package pl.prv.polanie.client.widgets.schedule.custom;

import pl.prv.polanie.client.model.UserSettings;
import pl.prv.polanie.client.widgets.CustomDialogBox;
import pl.prv.polanie.client.widgets.schedule.Schedule;
import pl.prv.polanie.client.widgets.schedule.SubjectEvent;

import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * @author Janusz Prokulewicz
 * Created: May 16, 2008
 * 
 * Kontrolka listujaca pokazujaca sie na formatce w momencie edycji ktoregos SubjectEventa.
 * Mozna w niej pokazac/schowac podpowiedzi dotyczace edytowanego SE.
 * 
 *  1 - wlacz/wylacz zajete przedzialy czasu ze wzgledu ba wykladowce 
 *	2 - wlacz/wylacz preferencje wykladowcy
 *	3 - pokaz/schowaj subjectEventy dla lecturera
 *	4 - pokaz/schowaj subjectEventy dla sali
 *  
 */
public class SubjectEventEditionWidget extends CustomDialogBox {
	
//	private static final String STYLE_SUBJECT_EVENT_EDITION_WIDGET = "my-PreferenceTypeChooser-item";
//	private static final String STYLE_PREFERENCE_TYPE_CHOOSE__ITEM_HOVERED = "my-PreferenceTypeChooser-itemHovered";

	private Schedule schedule = null;
	private SubjectEvent subjectEvent = null;

	MyButton lecturerPreferencesCB = null; 
	MyButton lecturerEventsCB = null;
	MyButton roomEventsCB = null;
	
	private VerticalPanel mainVP = new VerticalPanel();

	public SubjectEventEditionWidget(SubjectEvent se, Schedule schedule) {
		super(false, false);
		this.subjectEvent = se;
		this.schedule = schedule;
		
		mainVP.setBorderWidth(0);
		mainVP.setSpacing(0);
		mainVP.setWidth(150+"px");
		
		setWidget(mainVP);
	
		
		
		
		//lecturer preferences
		String lecturerPreferencesHtmlCode = "<table><tr><td><div style='width:16px; height: 16px; background:" + "url(images-schedule/preferences_icon.png)" + ";' >" +
		" </div></td><td> " + "lecturer preferences" + "</td></tr></table>";

		lecturerPreferencesCB = new MyButton(lecturerPreferencesHtmlCode);
		lecturerPreferencesCB.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				if (lecturerPreferencesCB.isChecked()) {
					UserSettings.setWhileSEEditionLoadLecturerPreferences(true);
					subjectEvent.loadSubjectEventLecturerPreferences();
				} else {
					UserSettings.setWhileSEEditionLoadLecturerPreferences(false);
					subjectEvent.hideSubjectEventLecturerPreferences();
				}
			}
		});
		mainVP.add(lecturerPreferencesCB);
		if (UserSettings.isWhileSEEditionLoadLecturerPreferences()) {
			lecturerPreferencesCB.setChecked(true);
			if (subjectEvent != null) {
				subjectEvent.loadSubjectEventLecturerPreferences();
			}
		} else {
			lecturerPreferencesCB.setChecked(false);
		}
		
		
		
		
		
		
		//lecturer events
		String lecturerEventsHtmlCode = "<table><tr><td><div style='width:16px; height: 16px; background-color:" + "#f2bcc1" + ";' >" +
		" </div></td><td> " + "lecturer events" + "</td></tr></table>";

		lecturerEventsCB = new MyButton(lecturerEventsHtmlCode);
		lecturerEventsCB.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				if (lecturerEventsCB.isChecked()) {
					UserSettings.setWhileSEEditionLoadLecturerEvents(true);
					subjectEvent.loadSubjectEventHintsForLecturer();
				} else {
					UserSettings.setWhileSEEditionLoadLecturerEvents(false);
					subjectEvent.hideSubjectEventHintsForLecturer();
				}
			}
		});
		mainVP.add(lecturerEventsCB);
		if (UserSettings.isWhileSEEditionLoadLecturerEvents()) {
			lecturerEventsCB.setChecked(true);
			if (subjectEvent != null) {
				subjectEvent.loadSubjectEventHintsForLecturer();
			}
		} else {
			lecturerEventsCB.setChecked(false);
		}
		


		//room events
		String roomEventsHtmlCode = "<table><tr><td><div style='width:16px; height: 16px; background-color:" + "#ffe2a6" + ";' >" +
		" </div></td><td> " + "room events" + "</td></tr></table>";
		
		roomEventsCB = new MyButton(roomEventsHtmlCode);
		roomEventsCB.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				if (roomEventsCB.isChecked()) {
					UserSettings.setWhileSEEditionLoadRoomEvents(true);
					subjectEvent.loadSubjectEventHintsForRoom();
				} else {
					UserSettings.setWhileSEEditionLoadRoomEvents(false);
					subjectEvent.hideSubjectEventHintsForRoom();
				}
			}
		});
		mainVP.add(roomEventsCB);
		if (UserSettings.isWhileSEEditionLoadRoomEvents()) {
			roomEventsCB.setChecked(true);
			if (subjectEvent != null) {
				subjectEvent.loadSubjectEventHintsForRoom();
			}
		} else {
			roomEventsCB.setChecked(false);
		}
		
		
//		CustomButton closeB = new CustomButton("close sa"){};
		
		
		
		Button closeB = new Button("close");
		closeB.setWidth("70px");
//		closeB.setHeight("18px");
		closeB.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				if (SubjectEventEditionWidget.this.schedule != null) {
					SubjectEventEditionWidget.this.schedule.hideSubjectEventEditionWidget();
					SubjectEventEditionWidget.this.schedule.setDisplayHintsWidgetDuringSEEdition(false);
				} else {
//					SubjectEventEditionWidget.this.hide();
				}
			}
		});
		mainVP.add(closeB);
		mainVP.setCellHeight(closeB, "26px");
		mainVP.setCellHorizontalAlignment(closeB, HasHorizontalAlignment.ALIGN_CENTER);
		mainVP.setCellVerticalAlignment(closeB, HasVerticalAlignment.ALIGN_MIDDLE);
		
		
		
		
		
		
		
		/**
		 *
		 *	zajecia wykladowcy
			preferencje wykladowcy
			zajecia odbywajace sie w sali
		 */
		
		
		
	}
	
	/**
	 * Na podstawie ustawieniach w kontrolce pokazuje podpowiedzi.
	 */
	public void applyUserSettingsToSchedule() {
		if (lecturerPreferencesCB.isChecked()) {
			subjectEvent.loadSubjectEventLecturerPreferences();
		}
		
		if (lecturerEventsCB.isChecked()) {
			subjectEvent.loadSubjectEventHintsForLecturer();
		}
		
		if (roomEventsCB.isChecked()) {
			subjectEvent.loadSubjectEventHintsForRoom();
		}
	}

	public void setSubjectEvent(SubjectEvent subjectEvent) {
		this.subjectEvent = subjectEvent;
	}

	public void setSchedule(Schedule schedule) {
		this.schedule = schedule;
	}

	public Schedule getSchedule() {
		return schedule;
	}
	
	@Override
	protected void runOnHidingWidget() {
		super.runOnHidingWidget();
		schedule.setDisplayHintsWidgetDuringSEEdition(false);
		
	}
	
	
	
}
